let number = document.getElementById('number')
let pageid = 0;
let totalCount = 0;
$.ajax({
    url: "http://chst.vip:1234/api/getmoneyctrl",
    data: {
        pageid
    }
}).then(res => {
    let html = ""
    let html2 = ""
    let totalCount = res.totalCount/10
    res.result.forEach(item => {
        html += `
        <li>
            <a href="./discount.html?productid=${item.productId}" productId="${item.productId}">
                <div class="img fl">
                    ${item.productImg2}
                </div>
                <div class="infoR">
                    <div class="title">
                        <h4>${item.productName}</h4>
                    </div>
                    <div class="other">
                        <span class="mall fl">
                            ${item.productFrom} | ${item.productTime}
                        </span>
                        <i class="iconfont icon-xiaoxi fr">
                            ${item.productComCount}
                        </i>
                    </div>
                </div>
            </a>
        </li>
        `
        
    })
    infoUl.innerHTML = html
    for (var i = 0; i < Math.ceil(totalCount); i++) {
        let num = i + 1
        html2 += `
        <option value="${num}">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            ${num}/${Math.ceil(totalCount)}
        </option>    
        `
    }
    number.innerHTML = html2

})

// console.log(number.value);
//页面跳转选择
number.onclick = function () {
    pageid = number.value - 1
    $.ajax({
        url: "http://chst.vip:1234/api/getmoneyctrl",
        data: {
            pageid
        }
    }).then(res => {
        let html = ""
        let totalCount = res.totalCount/10
        if (pageid === 0) {
            prev.disabled = "disabled"
            prev.style.color = "rgb(204, 204, 204)"
        } else if (pageid < 0) {
            return
        }
        if (pageid < Math.ceil(totalCount) - 1) {
            next.disabled = false
            next.style.color = "rgb(51, 51, 51)"
        }
        if (pageid === Math.ceil(totalCount) - 1) {
            next.disabled = "disabled"
            next.style.color = "rgb(204, 204, 204)"
        } else if (pageid > Math.ceil(totalCount) - 1) {
            return
        }
        if (pageid > 0) {
            prev.disabled = false
            prev.style.color = "rgb(51, 51, 51)"
        }
        res.result.forEach(item => {
            html += `
            <li>
                <a href="./discount.html?productid=${item.productId}" productId="${item.productId}">
                    <div class="img fl">
                        ${item.productImg2}
                    </div>
                    <div class="infoR">
                        <div class="title">
                            <h4>${item.productName}</h4>
                        </div>
                        <div class="other">
                            <span class="mall fl">
                                ${item.productFrom} | ${item.productTime}
                            </span>
                            <i class="iconfont icon-xiaoxi fr">
                                ${item.productComCount}
                            </i>
                        </div>
                    </div>
                </a>
            </li>
            `
        })
        infoUl.innerHTML = html
    })
}

//下一页点击事件
next.onclick = function () {
    pageid++
    $.ajax({
        url: "http://chst.vip:1234/api/getmoneyctrl",
        data: {
            pageid
        }
    }).then(res => {
        let html = ""
        let totalCount = res.totalCount/10
        if (pageid === Math.ceil(totalCount) - 1) {
            next.disabled = "disabled"
            next.style.color = "rgb(204, 204, 204)"
        } else if (pageid > Math.ceil(totalCount) - 1) {
            return
        }
        // console.log(pageid);
        if (pageid > 0) {
            prev.disabled = false
            prev.style.color = "rgb(51, 51, 51)"
        }
        res.result.forEach(item => {
            html += `
            <li>
                <a href="./discount.html?productid=${item.productId}" productId="${item.productId}">
                    <div class="img fl">
                        ${item.productImg2}
                    </div>
                    <div class="infoR">
                        <div class="title">
                            <h4>${item.productName}</h4>
                        </div>
                        <div class="other">
                            <span class="mall fl">
                                ${item.productFrom} | ${item.productTime}
                            </span>
                            <i class="iconfont icon-xiaoxi fr">
                                ${item.productComCount}
                            </i>
                        </div>
                    </div>
                </a>
            </li>
            `
        })
        infoUl.innerHTML = html

        //绑定option 联动
        $('.page').find('option:nth-child(' + (pageid + 1) + ')').prop('selected', true).siblings('option').prop('selected', false);
        
    })
}
//上一页点击事件
prev.onclick = function () {
    pageid--
    // console.log(pageid);
    $.ajax({
        url: "http://chst.vip:1234/api/getmoneyctrl",
        data: {
            pageid
        }
    }).then(res => {
        let html = ""
        let totalCount = res.totalCount/10
        if (pageid === 0) {
            prev.disabled = "disabled"
            prev.style.color = "rgb(204, 204, 204)"
        } else if (pageid < 0) {
            return
        }
        if (pageid < Math.ceil(totalCount) - 1) {
            next.disabled = false
            next.style.color = "rgb(51, 51, 51)"
        }
        res.result.forEach(item => {
            html += `
        <li>
            <a href="./discount.html?productid=${item.productId}" productId="${item.productId}">
                <div class="img fl">
                    ${item.productImg2}
                </div>
                <div class="infoR">
                    <div class="title">
                        <h4>${item.productName}</h4>
                    </div>
                    <div class="other">
                        <span class="mall fl">
                            ${item.productFrom} | ${item.productTime}
                        </span>
                        <i class="iconfont icon-xiaoxi fr">
                            ${item.productComCount}
                        </i>
                    </div>
                </div>
            </a>
        </li>
        `
        })
        infoUl.innerHTML = html
        $('.page').find('option:nth-child(' + (pageid + 1) + ')').prop('selected', true).siblings('option').prop('selected', false);
    })
}